package org.jeecg.modules.exZbwjBidEvaluationConfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpert;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertStage;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertService;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertStageService;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.mapper.ExSectionMapper;
import org.jeecg.modules.exTenderFile.entity.ExScoreStandard;
import org.jeecg.modules.exTenderFile.entity.ExZbwjConformanceReview;
import org.jeecg.modules.exTenderFile.service.IExScoreStandardService;
import org.jeecg.modules.exTenderFile.service.IExZbwjConformanceReviewService;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationConfig;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationItem;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.form.ExZbwjBidEvaluationConfigForm;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.vo.ExZbwjBidEvaluationConfigVO;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.vo.ExZbwjBidEvaluationItemVO;
import org.jeecg.modules.exZbwjBidEvaluationConfig.mapper.ExZbwjBidEvaluationConfigMapper;
import org.jeecg.modules.exZbwjBidEvaluationConfig.mapper.ExZbwjBidEvaluationItemMapper;
import org.jeecg.modules.exZbwjBidEvaluationConfig.service.IExZbwjBidEvaluationConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 开评标配置
 * @Author: jeecg-boot
 * @Date: 2024-10-24
 * @Version: V1.0
 */
@Service
public class ExZbwjBidEvaluationConfigServiceImpl extends ServiceImpl<ExZbwjBidEvaluationConfigMapper, ExZbwjBidEvaluationConfig> implements IExZbwjBidEvaluationConfigService {

    @Autowired
    private ExZbwjBidEvaluationConfigMapper exZbwjBidEvaluationConfigMapper;
    @Autowired
    private ExSectionMapper exSectionMapper;
    @Autowired
    private ExZbwjBidEvaluationItemMapper exZbwjBidEvaluationItemMapper;
    @Autowired
    private IExScoreStandardService exScoreStandardService;
    @Autowired
    private IExZbwjConformanceReviewService reviewService;
    @Autowired
    private IExPingbiaoExpertService iExPingbiaoExpertService;
    @Autowired
    private IExPingbiaoExpertStageService stageService;
    @Autowired
    private IExPingbiaoExpertStageService expertStageService;

    @Override
    public List<ExZbwjBidEvaluationConfigVO> selectByProjectId(String projectId) {
        List<ExSection> sectionList = exSectionMapper.selectByMainId(projectId);
        List<ExZbwjBidEvaluationConfig> list = exZbwjBidEvaluationConfigMapper.selectByProjectId(projectId);
        Map<String, ExZbwjBidEvaluationConfig> configMap = list.stream().collect(Collectors.toMap(ExZbwjBidEvaluationConfig::getBidSectionId, Function.identity()));

        Map<String, List<ExZbwjBidEvaluationItemVO>> itemMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> mainIdList = list.stream().map(ExZbwjBidEvaluationConfig::getId).collect(Collectors.toList());
            List<ExZbwjBidEvaluationItem> itemList = exZbwjBidEvaluationItemMapper.selectByMainIdList(mainIdList);
            List<ExZbwjBidEvaluationItemVO> itemVOList = Lists.newArrayList();
            for (ExZbwjBidEvaluationItem item : itemList) {
                ExZbwjBidEvaluationItemVO itemVO = new ExZbwjBidEvaluationItemVO();
                BeanUtils.copyProperties(item, itemVO);
                itemVOList.add(itemVO);
            }
            itemMap = itemVOList.stream().collect(Collectors.groupingBy(ExZbwjBidEvaluationItem::getBidEvaluationId));
        }

        List<ExZbwjBidEvaluationConfigVO> configList = Lists.newArrayList();
        for (ExSection e : sectionList) {
            ExZbwjBidEvaluationConfigVO configVO = new ExZbwjBidEvaluationConfigVO();
            configVO.setConfigFlag(Boolean.FALSE);
            ExZbwjBidEvaluationConfig config = configMap.get(e.getId());
            if (null != config) {
                configVO.setConfigFlag(Boolean.TRUE);
                BeanUtils.copyProperties(config, configVO);
                configVO.setItemList(itemMap.getOrDefault(config.getId(), Lists.newArrayList()));
            }
            configVO.setProjectId(e.getProjectId());
            configVO.setBidSectionId(e.getId());
            configVO.setBidSectionName(e.getBidSectionName());
            configVO.setBidSectionContent(e.getBidSectionContent());
            configList.add(configVO);
        }
        return configList;
    }

    @Override
    public ExZbwjBidEvaluationConfigVO selectByProjectIdAndSectionId(String sectionId,String expertId) {
        ExSection exSection = exSectionMapper.selectById(sectionId);
        ExZbwjBidEvaluationConfig config = exZbwjBidEvaluationConfigMapper.selectByBidSectionId(sectionId);


        Map<String, List<ExZbwjBidEvaluationItemVO>> itemMap = Maps.newHashMap();
        if (null != config) {
            List<ExZbwjBidEvaluationItem> itemList = exZbwjBidEvaluationItemMapper.selectByMainIdList(com.google.common.collect.Lists.newArrayList(config.getId()));
            List<String> businessIdList = itemList.stream().map(ExZbwjBidEvaluationItem::getId).collect(Collectors.toList());
            List<ExPingbiaoExpert> pingbiaoExpertList = iExPingbiaoExpertService.selectByExpertIdAndBusinessIdList(expertId, businessIdList);
            Map<String, List<ExPingbiaoExpert>> exPingbiaoExpertMap = pingbiaoExpertList.stream().collect(Collectors.groupingBy(ExPingbiaoExpert::getBusinessId));
            // 评分标准
            List<String> standardIdList = itemList.stream().filter(e -> Objects.equals(e.getType(), "1")).map(ExZbwjBidEvaluationItem::getBusinessId).collect(Collectors.toList());
            Map<String, String> standardNameMap = Maps.newHashMap();
            if (CollectionUtils.isNotEmpty(standardIdList)) {
                List<ExScoreStandard> standardList = exScoreStandardService.listByIds(standardIdList);
                standardNameMap = standardList.stream().collect(Collectors.toMap(ExScoreStandard::getId, ExScoreStandard::getName));
            }
            // 符合性审查项
            List<String> reviewIdList = itemList.stream().filter(e -> Objects.equals(e.getType(), "2")).map(ExZbwjBidEvaluationItem::getBusinessId).collect(Collectors.toList());
            Map<String, String> reviewNameMap = Maps.newHashMap();
            if (CollectionUtils.isNotEmpty(reviewIdList)) {
                List<ExZbwjConformanceReview> reviewList = reviewService.listByIds(reviewIdList);
                reviewNameMap = reviewList.stream().collect(Collectors.toMap(ExZbwjConformanceReview::getId, ExZbwjConformanceReview::getName));
            }

            List<ExZbwjBidEvaluationItemVO> itemVOList = Lists.newArrayList();
            for (ExZbwjBidEvaluationItem item : itemList) {
                ExZbwjBidEvaluationItemVO itemVO = new ExZbwjBidEvaluationItemVO();
                BeanUtils.copyProperties(item, itemVO);
                if (Objects.equals(item.getType(), "1")) {
                    itemVO.setBusinessName(standardNameMap.get(item.getBusinessId()));
                } else {
                    itemVO.setBusinessName(reviewNameMap.get(item.getBusinessId()));
                }
                List<ExPingbiaoExpert> exPingbiaoExpertList= exPingbiaoExpertMap.get(item.getId());
                itemVO.setCompleteStatus("0");
                if (CollectionUtils.isNotEmpty(exPingbiaoExpertList)) {
                    long count =exPingbiaoExpertList.stream().filter(e->!Objects.equals(e.getCompleteStatus(),"1")).count();
                    itemVO.setCompleteStatus(count == 0 ? "1" : "0");
                    itemVO.setCompleteTime(exPingbiaoExpertList.get(0).getCompleteTime());
                }
                itemVOList.add(itemVO);
            }
            itemMap = itemVOList.stream().collect(Collectors.groupingBy(ExZbwjBidEvaluationItem::getBidEvaluationId));
        }

        ExZbwjBidEvaluationConfigVO configVO = new ExZbwjBidEvaluationConfigVO();
        configVO.setConfigFlag(Boolean.FALSE);
        if (null != config) {
            configVO.setConfigFlag(Boolean.TRUE);
            BeanUtils.copyProperties(config, configVO);
            configVO.setItemList(itemMap.getOrDefault(config.getId(), Lists.newArrayList()));
        }
        configVO.setProjectId(exSection.getProjectId());
        configVO.setBidSectionId(exSection.getId());
        configVO.setBidSectionName(exSection.getBidSectionName());
        configVO.setBidSectionContent(exSection.getBidSectionContent());
        ExPingbiaoExpertStage exPingbiaoExpertStage = stageService.selectBySectionId(sectionId);
        configVO.setCurrentItemIndex(0);
        if (null != exPingbiaoExpertStage) {
            for (int i = 0; i < configVO.getItemList().size(); i++) {
                ExZbwjBidEvaluationItemVO itemVO = configVO.getItemList().get(i);
                if (Objects.equals(itemVO.getId(), exPingbiaoExpertStage.getBusinessId())) {
                    configVO.setCurrentItemIndex(i);
                }
            }
        }
        return configVO;
    }

    @Override
    public void save(ExZbwjBidEvaluationConfigForm configForm) {
        ExZbwjBidEvaluationConfig configInsert = new ExZbwjBidEvaluationConfig();
        BeanUtils.copyProperties(configForm, configInsert);
        exZbwjBidEvaluationConfigMapper.insert(configInsert);
        if (CollectionUtils.isNotEmpty(configForm.getItemList())) {
            for (int i = 0; i < configForm.getItemList().size(); i++) {
                ExZbwjBidEvaluationItem e = configForm.getItemList().get(i);
                ExZbwjBidEvaluationItem item = new ExZbwjBidEvaluationItem();
                item.setBidEvaluationId(configInsert.getId());
                item.setType(e.getType());
                item.setBusinessId(e.getBusinessId());
                item.setIndex(i);
                exZbwjBidEvaluationItemMapper.insert(item);
            }
        }
    }


    @Override
    public Result<String> update(ExZbwjBidEvaluationConfigForm configForm) {
        String bidSectionId= configForm.getBidSectionId();
        ExPingbiaoExpertStage exPingbiaoExpertStage= expertStageService.selectBySectionId(bidSectionId);
        if(null != exPingbiaoExpertStage){
            return Result.error("已经设置评标信息，不能修改");
        }

        ExZbwjBidEvaluationConfig configUpdate = new ExZbwjBidEvaluationConfig();
        BeanUtils.copyProperties(configForm, configUpdate);
        exZbwjBidEvaluationConfigMapper.updateById(configUpdate);
        exZbwjBidEvaluationItemMapper.deleteByMainId(configUpdate.getId());

        if (CollectionUtils.isNotEmpty(configForm.getItemList())) {
            for (int i = 0; i < configForm.getItemList().size(); i++) {
                ExZbwjBidEvaluationItem e = configForm.getItemList().get(i);
                ExZbwjBidEvaluationItem item = new ExZbwjBidEvaluationItem();
                item.setBidEvaluationId(configUpdate.getId());
                item.setType(e.getType());
                item.setBusinessId(e.getBusinessId());
                item.setIndex(i);
                exZbwjBidEvaluationItemMapper.insert(item);
            }
        }
        return Result.ok();
    }

    @Override
    public void deleteById(String id) {
        exZbwjBidEvaluationConfigMapper.deleteById(id);
        exZbwjBidEvaluationItemMapper.deleteByMainId(id);
    }
}
